﻿@model BBICMS.Model.ViewModel.Identity.ProfileAndRoleVM

@{
    ViewBag.PageTitle = "编辑" + EntityNames.User;
    ViewBag.Title = ViewBag.PageTitle + "_安全";
}

@section leftNav{
    @Html.Partial("_NavForSecurity")
}



<div class="main-content">
    <div class="row">
        <div class="col-sm-6">
            <div class="panel panel-default">
                <header class="panel-heading">
                    <span class="glyphicon glyphicon-edit"></span> @ViewBag.PageTitle
                </header>

                <section class="panel-body">
                    @Html.Partial("_EditProfile", Model.Profiles)
                </section>
            </div>
        </div>
        <div class="col-sm-6">
            <div class="panel panel-default">
                <header class="panel-heading">
                    <span class="glyphicon glyphicon-edit"></span> 权限设置
                </header>

                <section class="panel-body">
                    @{
                    var userRoles = Model.User.Roles.Select(r => r.Id);
                    }

                    @foreach (var role in Model.Roles)
            {
                var isChecked = @userRoles.Contains(role.Id) ? "checked" : "";

                        <div class="checkbox">
                            <label>
                                <input id="role_@role.Id"
                                       type="checkbox"
                                       name="ckRole"
                                       value="@role.Id"
                                       @isChecked />
                                @role.RoleName [@role.Description]

                            </label>
                        </div>
                        
                }



                </section>
            </div>
        </div>
    </div>
</div>




@section Footer {
    @Scripts.Render("~/bundles/jqueryval")
    <script>

        function updateSuccess(data) {

            if (data) {
                toastr.success('用户资料更新成功！', '@ViewBag.Title');

            } else {
                toastr.error('用户资料更新失败！', '@ViewBag.Title');
            }
        }


        $(function () {
            //当前链接
            $('.mainmenu>li:nth-of-type(1)').addClass('nav-open').find('.submenu li:nth-of-type(2) a').addClass('active');

            //删除数据
            $('[name="ckRole"]').change(function () {
                var isAdd = $(this).prop("checked"),
                    userId = $("#UserId").val(),
                    roleId = $(this).val(),
                    url = "../SetRole";

               // debugger;
                        $.ajax({
                            url: url,
                            dataType: "json",
                            type: "POST",
                            contentType: 'application/json; charset=utf-8',
                            data: JSON.stringify({ 'userId': userId, 'roleId': roleId, "isAdd": isAdd }),
                            async: true,
                            processData: false,
                            cache: false,
                            success: function (data) {
                                if (data) {
                                    toastr.success('角色设置成功！', '角色设置');

                                } else {
                                    toastr.error('角色设置失败！', '角色设置');
                                }
                            },
                            error: function (xhr) {
                                toastr.error('角色设置失败！', '角色设置');
                            }
                        });



            });
        });

    </script>
}